package com.psycho.sys.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.psycho.sys.api.common.Result;
import com.psycho.sys.api.entity.Appointment;
import com.psycho.sys.api.entity.ConsultationRecord;
import com.psycho.sys.api.entity.Counselor;
import com.psycho.sys.api.service.StudentConsultationService;
import com.psycho.sys.api.vo.AppointmentVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 学生心理咨询控制器
 */
@Tag(name = "学生心理咨询接口")
@RestController
@RequestMapping("/api/student/consultation")
public class StudentConsultationController {

    @Autowired
    private StudentConsultationService consultationService;

    @Operation(summary = "查看咨询师列表")
    @GetMapping("/counselors")
    public Result<List<Counselor>> listCounselors(
            @Parameter(description = "专业领域") @RequestParam(required = false) String specialization,
            @Parameter(description = "评分最低值") @RequestParam(required = false) Double minRating) {
        return Result.success(consultationService.listCounselors(specialization, minRating));
    }

    @Operation(summary = "发起预约请求")
    @PostMapping("/appointments")
    public Result<Boolean> createAppointment(@RequestBody Appointment appointment) {
        return Result.success(consultationService.createAppointment(appointment));
    }

    @Operation(summary = "查看我的预约")
    @GetMapping("/appointments")
    public Result<List<Appointment>> listMyAppointments(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "预约状态") @RequestParam(required = false) Integer status) {
        return Result.success(consultationService.listMyAppointments(userId, status));
    }

    @Operation(summary = "取消预约")
    @PutMapping("/appointments/{id}/cancel")
    public Result<Boolean> cancelAppointment(
            @PathVariable("id") Long appointmentId,
            @RequestParam String reason) {
        return Result.success(consultationService.cancelAppointment(appointmentId, reason));
    }

    @Operation(summary = "查看咨询记录")
    @GetMapping("/records")
    public Result<List<ConsultationRecord>> listMyConsultationRecords(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        return Result.success(consultationService.listMyConsultationRecords(userId));
    }

    @Operation(summary = "查看咨询详情")
    @GetMapping("/records/{id}")
    public Result<ConsultationRecord> getConsultationRecord(
            @PathVariable("id") Long recordId) {
        return Result.success(consultationService.getConsultationRecord(recordId));
    }

    @Operation(summary = "提交咨询评价")
    @PostMapping("/records/{id}/feedback")
    public Result<Boolean> submitFeedback(
            @PathVariable("id") Long recordId,
            @RequestParam Integer rating,
            @RequestParam String feedback) {
        return Result.success(consultationService.submitFeedback(recordId, rating, feedback));
    }

    @Operation(summary = "分页查询预约列表")
    @GetMapping("/appointments/page")
    public Result<Page<AppointmentVO>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户ID") @RequestParam(required = false) Long userId,
            @Parameter(description = "咨询师ID") @RequestParam(required = false) Long counselorId,
            @Parameter(description = "预约状态") @RequestParam(required = false) Integer status) {
        try {
            return Result.success(consultationService.pageAppointments(current, size, userId, counselorId, status));
        } catch (Exception e) {
            return Result.error("查询预约列表失败：" + e.getMessage());
        }
    }
}